﻿using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ApplicationServices;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class RolesController : Controller
    {
        // GET: Role
        private IPermissionService _permissionService;
        private IRoldService _roldService;
        public RolesController(IRoldService roldService, IPermissionService permissionService)
        {
            _roldService = roldService;
            _permissionService = permissionService;
        }

        // GET: Permission
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            List<PermissionDTO> perList = _permissionService.GetList();
            ViewBag.perList = perList;
            return View(perList);
        }
        [HttpPost]
        public ActionResult AddRole(RoIeEditDTO dto)
        {
            var result = _roldService.Add(dto);
            return Json(result);
        }
        public ActionResult Edit(long id)
        {
            RoIeEditDTO model = _roldService.Get(id);
            var perList = _permissionService.GetList();
            ViewBag.perList = perList;
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(long id)
        {
            var result = _roldService.Delete(id);
            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _roldService.DeleteBatch(ids);
            return Json(result);
        }
        [HttpPost]
        public ActionResult UpdateRole(RoldDTO dto)
        {
            AjaxResult result = _roldService.Update(dto);
            return Json(result);
        }

        [HttpPost]
        public ActionResult GetList(int start, int length, string name)
        {
            int count = 0;
            var list = _roldService.GetPagedList(start, length, name, ref count);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsFiltered = count;
            pageModel.recordsTotal = count;
            return Json(pageModel);
        }
    }
}